package handler;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import util.PageVO;
import biz.AreaBiz;
import entity.City;
import entity.Province;

@Controller
public class AreaHandler {
	@Autowired
	private AreaBiz areaBiz;
	
	@RequestMapping("province_query.do")
	@ResponseBody
	public Object queryProvince(String name,int page,int rows) {
		Map<String, Object> json=new HashMap<String, Object>();
		PageVO<Province> provinces = areaBiz.listProvincesByPage(name, page, rows);
		json.put("total",provinces.getTotalCount());
		json.put("rows", provinces.getResultList());
		return json;
	}
	
	@RequestMapping("province_list.do")
	@ResponseBody
	public Object listProvinces() {
		return areaBiz.listProvinces();
	}
	
	@RequestMapping("city_list.do")
	@ResponseBody
	public Object listCities(int provinceId) {
			return areaBiz.listCities(provinceId);
	}
	
	@RequestMapping("province_saveorupdate.do")
	@ResponseBody
	public int saveOrUpdateProvince(String action,Province province) {
		if("update".equals(action)){
			return areaBiz.updateProvince(province)?1:0;
		}else if("add".equals(action)){
			return areaBiz.addProvince(province)?1:0;
		}else{
			return 0;
		}
	}
	
	@RequestMapping("province_del.do")
	@ResponseBody
	public Object delProvince(int id) {
		if (areaBiz.delProvince(id)) {
			return "ok";
		}else{
			return "err";
		}
	}
	
	@RequestMapping("city_query.do")
	@ResponseBody
	public Object queryCity(int provinceId,String cityName,int page,int rows) {
		Map<String, Object> json=new HashMap<String, Object>();
		PageVO<City> Cities = areaBiz.listCitiesByPage(provinceId,cityName, page, rows);
		json.put("total",Cities.getTotalCount());
		json.put("rows", Cities.getResultList());
		return json;
	}
	
	@RequestMapping("city_update.do")
	@ResponseBody
	public String updateCity(City city) {
		if (areaBiz.updateCity(city)) {
			return "ok";
		}else{
			return "err";
		}
	}
	@RequestMapping("city_add.do")
	@ResponseBody
	public Object addCity(City city) {
		int id = areaBiz.addCity(city);
		if (id>0) {
			return id;
		}else{
			return "err";
		}
	}
	@RequestMapping("city_del.do")
	@ResponseBody
	public Object delCity(City city) {
		if (areaBiz.delCity(city)) {
			return "ok";
		}else{
			return "err";
		}
	}
}
